package com.baitiaojun.auth.api.impl;

import com.baitiaojun.api.domain.auth.UserAuthorities;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.auth.api.UserAuthoritiesApi;
import com.baitiaojun.auth.service.UserAuthoritiesService;
import com.baitiaojun.auth.service.UserRoleService;
import com.baitiaojun.auth.support.UserSupport;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * @类名 UserAuthoritiesApiImpl
 * @描述 TODO 多个任务并行执行
 * @作者 白条君
 * @创建日期 2022/12/7 20:57
 * @版本 1.0
 */
@RestController
public class UserAuthoritiesApiImpl implements UserAuthoritiesApi {

    @Resource
    private UserAuthoritiesService userAuthoritiesService;

    @Resource
    private UserSupport userSupport;

    @Resource
    private HttpServletRequest request;

    @Resource
    private UserRoleService userRoleService;

    @Override
    public JsonRes getUserAuthorities() throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        UserAuthorities userAuthorities = userAuthoritiesService.getUserAuthorities(userId);
        return JsonRes.builder().success().data(userAuthorities);
    }

    @Override
    public JsonRes getRoleCode(@RequestParam("userId") Long userId) {
        String roleCode = userRoleService.getRoleCodeByUserId(userId);
        return JsonRes.builder().success().data(roleCode);
    }

    @Override
    public JsonRes addDefaultRole(@RequestParam("userId") Long userId) throws ConditionException {
        userAuthoritiesService.addDefaultRole(userId);
        return JsonRes.builder().success();
    }
}
